package com.hxd.buybooksystem.ui.activity

import android.annotation.SuppressLint
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.QueryListener
import cn.bmob.v3.listener.SaveListener
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.hjq.image.ImageLoader
import com.hxd.buybooksystem.R
import com.hxd.buybooksystem.base.common.MyActivity
import com.hxd.buybooksystem.bean.BookInfo
import com.hxd.buybooksystem.bean.Order
import com.hxd.buybooksystem.uitls.L
import kotlinx.android.synthetic.main.activity_details_page.*
import org.jetbrains.anko.startActivity

class DetailsPageActivity : MyActivity() {

    private var book: BookInfo? = null
    private var objectId:String = ""

    override fun getLayoutId(): Int {
        return R.layout.activity_details_page
    }

    override fun getTitleId(): Int {
        return R.id.tb_title_DetailsPage
    }

    override fun initView() {
        btn_toBuyCar_details.setOnClickListener {
            addOrder()
        }
    }

    override fun initData() {
        initBookInfo()
    }

    /**
     * 新增一个订单数据
     */
    private fun addOrder() {
        //默认生成一个购买一本书的订单
        L.i("DetailsPageActivity : ObjectId $objectId")
        waitDialog
        val order = Order(objectId, book!!.name, book!!.price, book!!.count, 1, false,book!!.image)
        order.save(object : SaveListener<String>() {
            override fun done(objectId: String?, e: BmobException?) {
                waitDialog.dismiss()
                if (e == null) {
                    successDialog
                    L.i("添加订单成功，返回objectId为：$objectId")
                } else {
                    errorDialog
                    L.i("创建订单失败：" + e.errorCode)
                }
            }
        })
    }

    /**
     * 接收首页传递过来的数据
     */
    private fun initBookInfo() {
        objectId = intent.getStringExtra("objectId")
        if(objectId != ""){
            val bmobQuery = BmobQuery<BookInfo>()
            waitDialog
            bmobQuery.getObject(objectId, object : QueryListener<BookInfo>() {
                override fun done(bookInfo: BookInfo, e: BmobException?) {
                    waitDialog.dismiss()
                    if (e == null) {
//                       L.i("查询成功, $bookInfo")
                        book = bookInfo
                        updateUi(bookInfo)
                    } else {
                        errorDialog
                        L.i("查询失败：" + e.errorCode)
                    }
                }
            })
        }
    }

    /**
     * 更新UI界面
     */
    @SuppressLint("ResourceAsColor")
    private fun updateUi(bookInfo: BookInfo) {
        ImageLoader.loadImage(iv_image_details, bookInfo.image)
        tv_name_details.text = bookInfo.name
        tv_price_details.text = bookInfo.price
        tv_author_details.text = bookInfo.author
        tv_press_details.text = bookInfo.press
        tv_count_details.text = bookInfo.count.toString()
        if (bookInfo.count <= 0) {
            tv_count_details.setTextColor(R.color.red)
            btn_toBuyCar_details.setBackgroundColor(R.color.colorButtonDisable)
            btn_toBuyCar_details.isEnabled = false
        }
        tv_desc_details.text = bookInfo.desc
    }

    override fun onBackPressed() {
        startActivity<MainActivity>()
        finish()
    }

}
